define(function () {
  /**
   * 矩形标绘
   * @param {Cesium.Viewer} viewer 基本视图
   * @param {Cesium.ScreenSpaceEventHandler} handler 屏幕监听事件
   * @param {Cesium.CustomDataSource}plotEntities 标绘实体集合
   */
  let rectanglePlotting= function (viewer, handler, plotEntities) {
    let startearthPosition                              //开始点笛卡尔坐标
    let startcartographic                               //开始点弧度
    let startlat                                        //开始点纬度
    let startlng                                        //开始点精度
    let endearthPosition                                //结束点笛卡尔坐标
    let endcartographic                                 //结束点弧度
    let endlat                                          //结束点维度
    let endlng                                          //结束点经度
    let leftdown = false                                //鼠标左键是否点下

    //锁定转动
    viewer.scene.screenSpaceCameraController.enableRotate = false

    /**
     *添加矩形实体
     * @param {Number}startlat
     * @param {Number}startlng
     * @param {Number}endlat
     * @param {Number}endlng
     */
    function drawShape(startlat, startlng, endlat, endlng) {
      return plotEntities.entities.add({
        description: 'plot',
        rectangle: {
          coordinates: Cesium.Rectangle.fromDegrees(Math.min(startlng, endlng), Math.min(startlat, endlat), Math.max(startlng, endlng), Math.max(startlat, endlat)),
          material: Cesium.Color.RED.withAlpha(0.3),
        }
      })
    }

    //左键按下获取起始点
    handler.setInputAction((event) => {
      leftdown = true
      startearthPosition = viewer.camera.pickEllipsoid(event.position, viewer.scene.globe.ellipsoid)
      startcartographic = Cesium.Cartographic.fromCartesian(startearthPosition)
      startlat = Cesium.Math.toDegrees(startcartographic.latitude)
      startlng = Cesium.Math.toDegrees(startcartographic.longitude)
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN)

    //左键松下获取结束点
    handler.setInputAction(function (event) {
      if (leftdown === true) {

        //是否松下左键既可以转动
        //viewer.scene.screenSpaceCameraController.enableRotate = true

        endearthPosition = viewer.camera.pickEllipsoid(event.position, viewer.scene.globe.ellipsoid)
        endcartographic = Cesium.Cartographic.fromCartesian(endearthPosition)
        endlat = Cesium.Math.toDegrees(endcartographic.latitude)
        endlng = Cesium.Math.toDegrees(endcartographic.longitude)
        drawShape(startlat, startlng, endlat, endlng)
      }
      leftdown = false
    }, Cesium.ScreenSpaceEventType.LEFT_UP)

    //右键点击可以转动
    handler.setInputAction((event) => {
      viewer.scene.screenSpaceCameraController.enableRotate = true
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  }
  return rectanglePlotting
})